<?php
/**
 * Defines various theme specific functions,
 * Implements WordPress standard hooks
 * Enables various WordPress functionalities.
 *
 * @package Behold
 * @since Behold 1.0.0
 */
if ( ! isset( $content_width ) ) 
		$content_width = 715; 

		add_action( 'after_setup_theme', 'behold_setup' );

/**
 * Admin options page for the social media buttons
 */


if ( ! function_exists( 'behold_setup' ) ) {
	function behold_setup() {
		add_editor_style();
		add_theme_support( 'automatic-feed-links' );	
		
		// Add support for a variety of post formats
		add_theme_support( 'post-formats', array( 'audio', 'video', 'link', 'gallery', 'quote', 'image' ) ) ;
	}	
}

add_action( 'init', 'behold_register_nav_menus' );

function behold_register_nav_menus() {		
	register_nav_menu( 
		'header-menu', __( 'Header Menu', 'Behold' ) 
	);
}	

if ( function_exists( 'add_theme_support' ) ){
	add_theme_support( 'post-thumbnails' );
}

// Add in-head JS block for audio post format.
function behold_add_audio_support() {
	if ( ! is_singular() || ( is_singular() && 'audio' == get_post_format() ) ) {
?>
		<script type="text/javascript">
			AudioPlayer.setup( "<?php echo get_template_directory_uri(); ?>/swf/player.swf", {
				bg: "111111",
				leftbg: "111111",
				rightbg: "111111",
				track: "222222",
				text: "ffffff",
				lefticon: "eeeeee",
				righticon: "eeeeee",
				border: "222222",
				tracker: "eb374b",
				loader: "666666"
			});
		</script>
<?php }
}
add_action( 'wp_head', 'behold_add_audio_support' );

function behold_javascripts_with_jquery()
{
	// Register the script like this for a theme:
	wp_register_script( 'behold', get_template_directory_uri() . '/js/behold.js', array( 'jquery' ),'','yes' );
	
	// For either a plugin or a theme, you can then enqueue the script:
	wp_enqueue_script( 'behold' );
	wp_enqueue_script( 'audio-player', get_template_directory_uri() . '/js/audio-player.js', array( 'swfobject' ), '20120525' );
}
add_action( 'wp_enqueue_scripts', 'behold_javascripts_with_jquery' );

if ( ! function_exists( 'behold_get_post_comments' ) ) :
function behold_get_post_comments() {
		if ( comments_open() && ! post_password_required() ) :
			echo '<p class="comments-link">';
				comments_popup_link( _x( '<div> <span class="meta-type">Comments:</span> None </div>', 'Behold' ) , _x( '<div> <span class="meta-type">Comments:</span> 1 comment </div>', 'comments number', 'Behold' ), _x( '<div> <span class="meta-type">Comments:</span> % comments</div>', 'comments number', 'Behold' ) );
			echo '</p>';			
		endif;
}
endif;

if ( ! function_exists( 'behold_get_post_categories' ) ) :
function behold_get_post_categories() {
	$behold_categories_list = get_the_category_list(', ', '');
	
	if ( $behold_categories_list ):	
		printf( __( '<p><span class="meta-type">Categories:</span> %1$s</p>', 'Behold' ), $behold_categories_list );
	else:
		return false;
	endif;	
}
endif;
	
if ( ! function_exists( 'behold_get_post_tags' ) ) :
function behold_get_post_tags() {
	$behold_tags_list = get_the_tag_list('', ', ', '');
	
	if ( $behold_tags_list ):			
		printf( __( '<p><span class="meta-type">Tags:</span> %2$s</p>', 'Behold' ), 'Behold', $behold_tags_list );
	else:
		return false;
	endif;	
}
endif;

if ( ! function_exists( 'behold_posted_at_with_author' ) ) :

function behold_posted_at_with_author() {

	printf( __( '<div class="post-date-time"><span class="meta-type">Posted on</span> <a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date" >%4$s</span></a></div><div class="post-by"><span class="meta-type">By</span><br> <a href="%5$s" title="%6$s">%7$s</a></div>', 'Behold' ),
		esc_url( get_permalink() ),
		esc_attr( get_the_time() ),
		esc_attr( get_the_date( 'M j, Y' ) ),
		esc_html( get_the_date('M j, Y') ),
		esc_html( get_author_posts_url(get_the_author_meta( 'ID' )) ),
		esc_attr( sprintf( __( 'View all posts by %s', 'Behold' ), get_the_author() ) ),
		esc_html( get_the_author() )
	);
	
}
endif;

if ( ! function_exists( 'behold_get_edit_link' ) ) :
function behold_get_edit_link() {
	edit_post_link( __( 'Edit', 'Behold' ), '<span class="edit-link">', '</span>' );
}
endif;


if ( ! function_exists( 'behold_comment' ) ) :
function behold_comment( $comment, $args, $depth ) {
	
	$GLOBALS['comment'] = $comment;
	switch ( $comment->comment_type ) :
		case 'pingback' :
		case 'trackback' :
	?>
	<li class="post pingback">
		<p><?php _e( 'Pingback:', 'Behold' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( 'Edit', 'Behold' ), '<span class="edit-link">', '</span>' ); ?></p>
	<?php
			break;
		default :
	?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
		<div id="comment-<?php comment_ID(); ?>" class="comment">
			<?php
					$avatar_size = 48;
						if ( '0' != $comment->comment_parent )
						$avatar_size = 39;

					echo get_avatar( $comment, $avatar_size );
			?>
			<footer class="comment-meta">
				<div class="comment-author vcard">
					<?php
						/* translators: 1: comment author, 2: date and time */
						printf( __( '%1$s on %2$s <span class="says">said:</span>', 'Behold' ),
							sprintf( '<span class="fn">%s</span>', get_comment_author_link() ),
							sprintf( '<a href="%1$s"><span>%3$s</span></a>',
								esc_url( get_comment_link( $comment->comment_ID ) ),
								get_comment_time( 'c' ),
								/* translators: 1: date, 2: time */
								sprintf( __( '%1$s at %2$s', 'Behold' ), get_comment_date(), get_comment_time() )
							)
						);
					edit_comment_link( __( 'Edit', 'Behold' ), '<span class="edit-link">', '</span>' ); ?>
				</div><!-- .comment-author .vcard -->

				<?php if ( $comment->comment_approved == '0' ) : ?>
					<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'Behold' ); ?></em>
					<br />
				<?php endif; ?>

			</footer>

			<div class="comment-content"><?php comment_text(); ?></div>

			<div class="reply">
				<?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply <span>&darr;</span>', 'Behold' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
			</div><!-- .reply -->
		</div><!-- #comment-## -->

	<?php
			break;
	endswitch;
}
endif; // ends check for behold_comment()

if ( ! function_exists( 'behold_post_navigation' ) ) :
/**
 * Display navigation to next/previous pages when applicable
 */
function behold_post_navigation( $nav_id ) {
	global $wp_query;

	if ( $wp_query->max_num_pages > 1 ) : ?>
		<div id="<?php echo $nav_id; ?>" class="group">
			<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'Behold' ) ); ?></div>
			<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'Behold' ) ); ?></div>
		</div><!-- $nav_id-->
	<?php endif;
}
endif; // behold_post_navigation

global $wp_version;

function behold_next_prev_post_titles( $nav_id ) {
?>
<div id="<?php echo $nav_id; ?>" class="group">
	<div class="nav-previous"> <?php previous_post_link( __( '<span class="meta-nav">&larr;</span> %link', 'Behold' ) );?></div>
	<div class="nav-next"> <?php next_post_link( __( '<span class="meta-nav"></span>  %link &rarr;', 'Behold' ) ); ?></div>
</div><!-- $nav_id-->
<?php
} # Close of behold_next_prev_post_titles

function behold_widgets_init() {
		
	register_sidebar( array(
		'name' => __( 'Toggleable right sidebar', 'Behold' ),
		'id' => 'sidebar-1',
		'description' => __( 'Optional: Appears on the right. Widgets will be toggleable. i.e visitors can expand/collapse the widget item.', 'Behold' ),
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => "</div>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	));	
	
	register_sidebar( array(
		'name' => __( 'Main Sidebar', 'Behold' ),
		'id' => 'sidebar-2',
		'description' => __( 'Optional: Appears on the right side of the page. Just a normal right sidebar.', 'Behold' ),
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => "</div>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	));
	
	register_sidebar( array(
		'name' => __( 'Footer Sidebar', 'Behold' ),
		'description' => __( 'Optional: Appears on the footer area of the web page', 'Behold' ),
		'id' => 'sidebar-3',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => "</div>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	));
	
	
	register_sidebar( array(
		'name' => __( 'Footer Text Widget Area', 'Behold' ),
		'description' => __( 'Optional: Use only the text-widget - appears inside the footer area, this is the first widget displayed even though the other footer widget area is used.', 'Behold' ),
		'id' => 'sidebar-4',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => "</div>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	));	
}

add_action( 'widgets_init', 'behold_widgets_init' );

// Add support for custom headers.

register_default_headers( array(
		
		'hexagons' => array(
			'url' => '%s/images/hello.png',
			'thumbnail_url' => '%s/images/hello.png',
			/* translators: header image description */
			'description' => __( 'Smiley!', 'Behold' )
		))
	);
	
	$custom_header_support = array(
		// The default header text color.
		'default-text-color' => '000',
		// The height and width of our custom header.
		'width' => '970',
		'height' => '320',
		// Random image rotation by default.
		'random-default' => true		
	);


	global $wp_version;
	if ( version_compare( $wp_version, '3.4', '>=' ) ) :
		add_theme_support( 'custom-header' );
		add_theme_support( 'custom-header', $custom_header_support );
	endif;

function behold_new_excerpt_more($more) {
	global $post;
	return ' <a href="'. esc_url( get_permalink() ) . '">' . __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) . '</a>';
}
add_filter('excerpt_more', 'behold_new_excerpt_more');

function behold_get_featured_image() {
	global $post;
	if ( has_post_thumbnail( $post->ID ) ) {
		echo get_the_post_thumbnail( $post->ID, 'medium' );
	}
}
if ( ! function_exists( 'behold_audio_grabber' ) ) {
/**
 * Credit to - Pinktouch WP Theme! I could have written my own, thought why reinvent the wheel.. :)
 * Return the first audio file found for a post.
 *
 * @param int post_id ID for parent post
 * @return boolean|string Path to audio file
 */
function behold_audio_grabber( $post_id ) {
	global $wpdb;

	$first_audio = $wpdb->get_var( $wpdb->prepare( "SELECT guid FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'attachment' AND INSTR(post_mime_type, 'audio') ORDER BY menu_order ASC LIMIT 0,1", (int) $post_id ) );
    
	# debug
	# printf("SELECT guid FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'attachment' AND INSTR(post_mime_type, 'audio') ORDER BY menu_order ASC LIMIT 0,1", (int) $post_id );
	
	if ( ! empty( $first_audio ) )
		return $first_audio;

	return false;
}
}

function behold_theme_menu() {  
    add_theme_page(  
        'behold_ Theme Options', 			// The title to be displayed in the browser window for this page.  
        'Social Media Options',            		// The text to be displayed for this menu item  
        'edit_theme_options',            		// Which type of users can see this menu item  
        'behold_theme_options',    			// The unique ID - that is, the slug - for this menu item  
        'behold_theme_display'     			// The name of the function to call when rendering this menu's page  
    );  
} 
add_action('admin_menu', 'behold_theme_menu');


function behold_theme_intialize_social_options() {   
    if( false == get_option( 'behold_theme_social_options' ) ) {  
        add_option( 'behold_theme_social_options' );  
    } 
	
	add_settings_section(  
		'social_settings_section',          // ID used to identify this section and with which to register options  
		'Social Media Buttons',             // Title to be displayed on the administration page  
		'behold_social_options_callback',  // Callback used to render the description of the section  
		'behold_theme_social_options'      // Page on which to add this section of options  
	);

	add_settings_field(  
        'facebook',  
        'Facebook',  
        'behold_facebook_callback',  
        'behold_theme_social_options',  
        'social_settings_section'  
    );
	
	add_settings_field(  
		'twitter',  
		'Twitter',  
		'behold_twitter_callback',  
		'behold_theme_social_options',  
		'social_settings_section'  
	);  
	
    add_settings_field(  
        'googleplus',  
        'Google+',  
        'behold_googleplus_callback',  
        'behold_theme_social_options',  
        'social_settings_section'  
    ); 
	
	add_settings_field(  
        'pinterest',  
        'Pinterest',  
        'behold_pinterest_callback',  
        'behold_theme_social_options',  
        'social_settings_section'  
    ); 
	
	add_settings_field(  
        'linkedin',  
        'LinkedIn',  
        'behold_linkedin_callback',  
        'behold_theme_social_options',  
        'social_settings_section'  
    ); 
	
	add_settings_field(  
        'rss',  
        'RSS',  
        'behold_rss_callback',  
        'behold_theme_social_options',  
        'social_settings_section'  
    );
	
	register_setting(  
		'behold_theme_social_options',  
		'behold_theme_social_options',  
		'behold_theme_sanitize_social_options'  
	);
} 
add_action( 'admin_init', 'behold_theme_intialize_social_options' ); 

function behold_social_options_callback() {
	_e('<p>Provide the URL to the social networks you\'d like to display. Leave it blank if you don\'t want to display the button.</p>', 'Behold');
} 

function behold_facebook_callback() { 
    // First, we read the social options collection
    $options = get_option( 'behold_theme_social_options' );  
    // Next, we need to make sure the element is defined in the options. If not, we'll set an empty string.  
    $url = ''; 
    if( !isset( $options['facebook'] ) ) { 
		$options['facebook'] = '';
	} else {
        $url = $options['facebook']; 
    } // end if 
    // Render the output 
    echo '<input type="text" id="facebook" name="behold_theme_social_options[facebook]" value="' . $options['facebook'] . '" />';  
}


function behold_twitter_callback() {  
    // First, we read the social options collection  
    $options = get_option( 'behold_theme_social_options' ); 
    // Next, we need to make sure the element is defined in the options. If not, we'll set an empty string.  
    $url = '';
    if( !isset( $options['twitter'] ) ) { 
		$options['twitter'] = '';
	} else {
        $url = $options['twitter']; 
    } // end if 
    // Render the output 
    echo '<input type="text" id="twitter" name="behold_theme_social_options[twitter]" value="' . $options['twitter'] . '" />';  
}

function behold_googleplus_callback() {  
    // First, we read the social options collection  
    $options = get_option( 'behold_theme_social_options' );
    // Next, we need to make sure the element is defined in the options. If not, we'll set an empty string.  
    $url = ''; 
    if( !isset( $options['googleplus'] ) ) {
		$options['googleplus'] = '';
	} else {
        $url = $options['googleplus']; 
    } // end if 
    // Render the output 
    echo '<input type="text" id="googleplus" name="behold_theme_social_options[googleplus]" value="' . $options['googleplus'] . '" />';  
}

function behold_pinterest_callback() {  
    // First, we read the social options collection  
    $options = get_option( 'behold_theme_social_options' ); 
    // Next, we need to make sure the element is defined in the options. If not, we'll set an empty string.  
    $url = ''; 
    if( !isset( $options['pinterest'] ) ) {
		$options['pinterest'] = '';
	} else {
        $url = $options['pinterest']; 
    } // end if 
    // Render the output 
    echo '<input type="text" id="pinterest" name="behold_theme_social_options[pinterest]" value="' . $options['pinterest'] . '" />';  
}

function behold_linkedin_callback() { 
    // First, we read the social options collection  
    $options = get_option( 'behold_theme_social_options' );  
    // Next, we need to make sure the element is defined in the options. If not, we'll set an empty string.  
    $url = '';
    if( !isset( $options['linkedin'] ) ) { 
		$options['linkedin'] = '';
	} else {
        $url = $options['linkedin']; 
    } // end if 
    // Render the output 
    echo '<input type="text" id="linkedin" name="behold_theme_social_options[linkedin]" value="' . $options['linkedin'] . '" />';  
}

function behold_rss_callback() {  
    // First, we read the social options collection  
    $options = get_option( 'behold_theme_social_options' ); 
    // Next, we need to make sure the element is defined in the options. If not, we'll set an empty string.  
    $url = '';
    if( !isset( $options['rss'] ) ) {
		$options['rss'] = '';
	} else {
        $url = $options['rss']; 
    } // end if 
    // Render the output 
    echo '<input type="text" id="rss" name="behold_theme_social_options[rss]" value="' . $options['rss'] . '" />';  
}

function behold_theme_sanitize_social_options( $input ) {  
    // Define the array for the updated options  
    $output = array();  
    // Loop through each of the options sanitizing the data  
    foreach( $input as $key => $val ) {  
        if( isset ( $input[$key] ) ) {  
            $output[$key] = esc_url_raw( strip_tags( stripslashes( $input[$key] ) ) );  
        } // end if  
    } // end foreach  
    // Return the new collection  
    return apply_filters( 'behold_theme_sanitize_social_options', $output, $input );  
}


function behold_theme_display() {  
?>  
	<!-- Create a header in the default WordPress 'wrap' container -->  
	<div class="wrap">  
		<!-- Add the icon to the page -->  
		<div id="icon-themes" class="icon32"></div>
		<h2><?php _e('Behold Theme Options', 'Behold'); ?></h2>  
		<!-- Make a call to the WordPress function for rendering errors when settings are saved. -->  
		<?php settings_errors(); ?>  
		<!-- Create the form that will be used to render our options -->  
		<form method="post" action="options.php">  
			    <?php settings_fields( 'behold_theme_social_options' ); ?>  
    			<?php do_settings_sections( 'behold_theme_social_options' ); ?>   
			<?php submit_button(); ?>  
		</form>  
	</div><!-- /.wrap -->  
<?php  
} // end behold_theme_display  

function behold_shorter_excerpt() {
	$excerpt_length = 80;
	$the_excerpt = get_the_excerpt();
	
	if ( strlen($the_excerpt) > $excerpt_length ) {
		echo "<p>" . substr( $the_excerpt, 0, $excerpt_length ) . "...</p>";
	}
}